package com.bailian.system.handler;

import com.bailian.system.mapper.BlParkTagsMapper;
import com.bailian.system.service.IBlParkTagsService;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import com.bailian.system.domain.BlParkTags;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 仓库标签类型处理器
 * 用于处理数据库JSON字段与BlParkTags List之间的转换
 * 
 * @author bailian
 * @date 2025-08-08
 */
@MappedTypes(List.class)
public class WarehouseTagsTypeHandler extends BaseTypeHandler<List<BlParkTags>> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<BlParkTags> parameter, JdbcType jdbcType) throws SQLException {
        if (parameter != null) {
            // 将BlParkTags列表转换为ID列表
            List<Long> idList = new ArrayList<>();
            for (BlParkTags tag : parameter) {
                if (tag != null && tag.getTagId() != null) {
                    idList.add(tag.getTagId());
                }
            }
            ps.setString(i, JSON.toJSONString(idList));
        } else {
            ps.setNull(i, jdbcType.TYPE_CODE);
        }
    }

    @Override
    public List<BlParkTags> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        return parseWarehouseTags(value);
    }

    @Override
    public List<BlParkTags> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        return parseWarehouseTags(value);
    }

    @Override
    public List<BlParkTags> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        return parseWarehouseTags(value);
    }

    /**
     * 解析JSON字符串为BlParkTags列表
     * 
     * @param value 数据库中的值
     * @return 解析后的BlParkTags列表
     */
    private List<BlParkTags> parseWarehouseTags(String value) {
        if (value == null || value.trim().isEmpty()) {
            return new ArrayList<>();
        }
        
        try {
            // 解析JSON数组为Long列表
            List<Long> idList = JSON.parseArray(value, Long.class);
            if (idList == null || idList.isEmpty()) {
                return new ArrayList<>();
            }
            
            // 将ID列表转换为BlParkTags对象列表
            List<BlParkTags> tagsList = new ArrayList<>();
            for (Long tagId : idList) {
                BlParkTags tag = new BlParkTags();
                tag.setTagId(tagId);
                tagsList.add(tag);
            }
            return tagsList;
        } catch (JSONException e) {
            // 如果解析失败，返回空列表
            return new ArrayList<>();
        }
    }
} 